import { BaseViewCmp } from "./BaseViewCmp";
import UI_cmp_scene3d from "../../../ui/Main/UI_cmp_scene3d";
import { Data_Scene3D } from "../../debug_inject/common/Data_Scene3D";
import { EObjCmpType } from "../../debug_inject/common/EObjCmpType";
import { BaseCmpData } from "../../debug_inject/common/BaseCmpData";
import { CmpTransform3D } from "./CmpTransform3D";
import { PropToggle } from "../prop/PropToggle";
import { DebuggerManager } from "../../debugger/DebuggerManager";
import { PropColor } from "../prop/PropColor";
import { Data_Vec3 } from "../../debug_inject/common/Data_Vec3";
import { PropNumber } from "../prop/PropNumber";

export class CmpScene3D extends BaseViewCmp {

    public ui: UI_cmp_scene3d;

    private _cacheData: Data_Scene3D;

    private _toggle_fogEnable: PropToggle;
    private _color_fogColor: PropColor;
    private _num_fogStart: PropNumber;
    private _num_fogRange: PropNumber;

    constructor(objId: number) {
        super(EObjCmpType.Scene3D, objId);
        this.ui = UI_cmp_scene3d.createInstance();
        this._toggle_fogEnable = new PropToggle(this.ui.m_view_enableFog.m_toggle);
        this._toggle_fogEnable.onToggleChanged = Laya.Handler.create(this, this._OnFogEnableChanged, null, false);

        this._color_fogColor = new PropColor(this.ui.m_view_fogDetail.m_view_fogColor);
        this._color_fogColor.onColorChanged = Laya.Handler.create(this, this._OnFogColorChanged, null, false);

        this._num_fogStart = new PropNumber(this.ui.m_view_fogDetail.m_view_fogStart);
        this._num_fogStart.onNumChanged = Laya.Handler.create(this, this._OnFogStartChanged, null, false);

        this._num_fogRange = new PropNumber(this.ui.m_view_fogDetail.m_view_fogRange);
        this._num_fogRange.onNumChanged = Laya.Handler.create(this, this._OnFogRangeChanged, null, false);
    }

    private _OnFogRangeChanged(num: number) {
        this._cacheData.fogRange = num;
        DebuggerManager.instance.SetFogRange(this.objId, num);
        this._RefreshUI();
    }

    private _OnFogStartChanged(num: number) {
        this._cacheData.fogStart = num;
        DebuggerManager.instance.SetFogStart(this.objId, num);
        this._RefreshUI();
    }

    private _OnFogColorChanged(colorVec3: Data_Vec3) {
        this._cacheData.fogColor = colorVec3;
        DebuggerManager.instance.SetFogColor(this.objId, colorVec3);
        this._RefreshUI();
    }

    private _OnFogEnableChanged(enableFog: boolean) {
        this._cacheData.enableFog = enableFog;
        DebuggerManager.instance.SetEnableFog(this.objId, this._cacheData.enableFog);
        this._RefreshUI();
    }

    _OnDispose() {
        this._toggle_fogEnable.Dispose();
        this._color_fogColor.Dispose();
        this._num_fogRange.Dispose();
        this._num_fogStart.Dispose();
    }

    _OnRefresh(data: BaseCmpData) {
        if (this._cacheData != null) {
            let oldJson = JSON.stringify(this._cacheData);
            let newJson = JSON.stringify(data.data);
            if (oldJson == newJson) {
                return;
            }
        }
        this._cacheData = data.data;

        this._RefreshUI();
    }

    private _RefreshUI() {
        let totalHeight = 70;
        this._toggle_fogEnable.isChecked = this._cacheData.enableFog;
        if (this._cacheData.enableFog) {
            totalHeight += 110;
            this.ui.m_view_fogDetail.visible = true;
            this._color_fogColor.SetColorVec3(this._cacheData.fogColor);
            this._num_fogStart.SetNum(this._cacheData.fogStart);
            this._num_fogRange.SetNum(this._cacheData.fogRange);
        } else {
            this.ui.m_view_fogDetail.visible = false;
        }

        this.ui.setSize(this.ui.width, totalHeight);
    }

}